//
//  CYGeRenZhuYeHeaderView.m
//  cyydt
//
//  Created by chu on 2019/1/27.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYGeRenZhuYeHeaderView.h"

@implementation CYGeRenZhuYeHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self addSeeMoreButton];
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.height / 2;
    self.headImageView.layer.masksToBounds = YES;
    
    self.guanzhuBtn.layer.cornerRadius = 5;
    self.guanzhuBtn.layer.masksToBounds = YES;
    
    
    self.stateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.headImageView.frame.size.width - 17 + 15, self.headImageView.frame.size.height - 17 + Height_NavBar, 17, 17)];
    [self.backView addSubview:self.stateImageView];
    [self.backView bringSubviewToFront:self.stateImageView];
}

- (void)addSeeMoreButton {
    
    __weak typeof(self) _self = self;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...展开"];
    
    YYTextHighlight *hi = [YYTextHighlight new];
    [hi setColor:RGBOF(0x4173AF)];
    hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        YYLabel *label = _self.introLabel;
        [_self addShouQi];

        [label sizeToFit];
        _self.bottomViewHeightConstraint.constant = label.frame.size.height + 25;
        _self.introLabelHeightConstraint.constant = label.frame.size.height;
        NSLog(@"label.frame.size.height - %f",label.frame.size.height);
        if (_self.block) {
            _self.block(label.frame.size.height + 25);
        }
    };
    
    [text yy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"展开"]];
    [text yy_setTextHighlight:hi range:[text.string rangeOfString:@"展开"]];
    text.yy_font = self.introLabel.font;
    
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = text;
    [seeMore sizeToFit];
    
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];
    self.introLabel.truncationToken = truncationToken;
}

- (void)addShouQi{
    
    __weak typeof(self) _self = self;
    NSString *value = [NSString stringWithFormat:@"%@收起", self.introLabel.attributedText.string];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:value];
    text.yy_lineSpacing = 8;
    text.yy_font = [UIFont systemFontOfSize:14];
    [text yy_setTextHighlightRange:NSMakeRange(self.introLabel.attributedText.string.length, 2) color:RGBOF(0x4173AF) backgroundColor:[UIColor whiteColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        YYLabel *label = _self.introLabel;
        
        _self.bottomViewHeightConstraint.constant = 17 + 25;
        _self.introLabelHeightConstraint.constant = 17;
        NSLog(@"label.frame.size.height - %f",label.frame.size.height);
        if (_self.block) {
            _self.block(17 + 25);
        }
        [label sizeToFit];
        NSString *value = [_self.introLabel.attributedText.string substringWithRange:NSMakeRange(0, _self.introLabel.attributedText.string.length - 2)];
        NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:value];
        introText.yy_font = [UIFont systemFontOfSize:14];
        introText.yy_lineSpacing = 8;
        _self.introLabel.attributedText = introText;
    }];
    
    self.introLabel.attributedText = text;
}

- (IBAction)guanzhuCountAction:(UIButton *)sender {
    
    if (self.fensiBlock) {
        self.fensiBlock(1);
    }
}

- (IBAction)fensiAction:(UIButton *)sender {
    if (self.fensiBlock) {
        self.fensiBlock(2);
    }
}

- (IBAction)guanzhuAction:(UIButton *)sender {
    if (self.guanzhuBlock) {
        self.guanzhuBlock();
    }
}

@end
